/***************************************************************************************************
Table A4: Transactions (net purchases) across sectors
***************************************************************************************************/
foreach yyyy of numlist 1994/2019{
	import delimited "$MyPath/Data/Micro Data/sum_stats_transactions_`yyyy'", clear
	gen year = `yyyy'
	keep year count
	rename count num_mean
	keep if _n == 1
	tempfile temp`yyyy'
	save `temp`yyyy''
}
drop _all
foreach yyyy of numlist 1994/2019{
	append using `temp`yyyy''
}
tempfile temp
save `temp'

use "$MyPath/save/sector_consolidated", clear
keep year seci *_cons
rename *_cons *
merge m:1 year using `temp',  nogen keep(master matched)
foreach v of varlist nt_housing* debt* deposits* nt_equity*{
	replace `v' = `v' * 1e6 / num_mean
}
* average across all years
collapse (mean) nt_housing* debt* deposits* nt_equity* num_mean, by(seci)
gather nt_housing* debt* deposits* nt_equity*
gen type = string(seci)
replace value = - value if regexm(variable, "debt")
drop seci
reshape wide value, i(num_mean variable) j(type) string
gen label = ""
gen order = .
local i = 0
replace label = "Housing" if variable == "nt_housing"
replace order = `++i' if variable == "nt_housing"
replace label = "\noalign{\vskip 1.5mm} Debt" if variable == "debt"
replace order = `++i' if variable == "debt"
replace label = "\quad Household debt" if variable == "debt_household"
replace order = `++i' if variable == "debt_household"
replace label = "\quad Corporate debt" if variable == "debt_corporate"
replace order = `++i' if variable == "debt_corporate"
replace label = "\quad Government debt" if variable == "debt_government"
replace order = `++i' if variable == "debt_government"
replace label = "\quad Foreign debt" if variable == "debt_foreign"
replace order = `++i' if variable == "debt_foreign"
replace label = "\noalign{\vskip 1.5mm} Deposits" if variable == "deposits"
replace order = `++i' if variable == "deposits"
replace label = "\quad Corporate deposits" if variable == "deposits_corporate"
replace order = `++i' if variable == "deposits_corporate"
replace label = "\quad Government deposits" if variable == "deposits_government"
replace order = `++i' if variable == "deposits_government"
replace label = "\quad Foreign deposits" if variable == "deposits_foreign"
replace order = `++i' if variable == "deposits_foreign"
replace label = "\noalign{\vskip 1.5mm} Equity" if variable == "nt_equity"
replace order = `++i' if variable == "nt_equity"
replace label = "\quad Corporate equity" if variable == "nt_equity_corporate"
replace order = `++i' if variable == "nt_equity_corporate"
replace label = "\quad Foreign equity" if variable == "nt_equity_foreign"
replace order = `++i' if variable == "nt_equity_foreign"
sort order
gen total = abs(value1 + value2 + value3)
assert total < 1e-4
format value* total %3.1fc
listtab label value1 value2 value3  total if !missing(order) using "$MyPath/tables/TableA4.tex", rstyle(tabular)  replace

/***************************************************************************************************
Output welfare datasets
***************************************************************************************************/
import delimited "$MyPath/Data/Micro Data/year_cohort", clear
keep if year == 1994
collapse (sum) num_mean
sum num_mean
local N = r(mean)

foreach method in "homogeneous" "heterogeneous"{
	use "$MyPath/save/sector_consolidated", clear
	keep year seci *_cons
	rename *_cons *
	merge m:1 year using "$MyPath/Save/prices", keep(master matched) nogen
	keep if inrange(year, 1995, 2020)

	* Housing
	egen welfare_housing = sum(-R0t_adhoc * nt_housing * PDh_shock), by(seci)

	* Debt
	egen welfare_debt_household = sum(R0t_adhoc * debt_household * Qm_shock), by(seci)
	egen welfare_debt_government = sum(R0t_adhoc * debt_government * Qm_shock), by(seci)
	egen welfare_debt_corporate = sum(R0t_adhoc * debt_corporate * Qm_shock), by(seci)
	if "`method'" == "homogeneous"{
		egen welfare_debt_foreign = sum(R0t_adhoc * debt_foreign * Qd_world_shock), by(seci)
	}
	else{
		egen welfare_debt_foreign = sum(R0t_adhoc * debt_foreign * Qm_shock), by(seci)
	}
	gen welfare_debt = welfare_debt_household + welfare_debt_government + welfare_debt_corporate + welfare_debt_foreign

	* Deposit
	egen welfare_deposits_government = sum(-R0t_adhoc * deposits_government * Qd_shock), by(seci)
	egen welfare_deposits_corporate = sum(-R0t_adhoc * deposits_corporate * Qd_shock), by(seci)
	if "`method'" == "homogeneous"{
		egen welfare_deposits_foreign = sum(-R0t_adhoc * deposits_foreign * Qd_shock), by(seci)
	}
	else{
		egen welfare_deposits_foreign = sum(-R0t_adhoc * deposits_foreign * Qd_world_shock), by(seci)
	}
	gen welfare_deposits = welfare_deposits_government + welfare_deposits_corporate + welfare_deposits_foreign

	* Equity
	egen welfare_equity_corporate = sum(-R0t_adhoc * nt_equity_corporate * PDe_shock), by(seci)
	if "`method'" == "homogeneous"{
		egen welfare_equity_foreign = sum(-R0t_adhoc * nt_equity_foreign * PDe_shock), by(seci)
	}
	else{
		egen welfare_equity_foreign = sum(-R0t_adhoc * nt_equity_foreign * PDe_world_shock), by(seci)
	}
	gen welfare_equity = welfare_equity_corporate + welfare_equity_foreign

	* Total
	gen welfare_total = welfare_housing + welfare_debt + welfare_deposits + welfare_equity

	keep if year == 1995
	foreach v of varlist welfare* {
		replace `v'= `v' * 1e6 / `N'
	}
	keep seci welfare*

	save "$MyPath/save/bysector_welfare_`method'", replace
}

/***************************************************************************************************
Figure A4: Welfare gains across sectors
***************************************************************************************************/

use "$MyPath/save/bysector_welfare_homogeneous"
gen g = 0 if seci == 1
replace g = 1 if seci == 2
replace g = 2 if seci == 3
gen g0 = g * 6
gen g1 = g0 + 1
gen g2 = g0 + 2
gen g3 = g0 + 3
gen g4 = g0 + 4

twoway ///
(bar welfare_total g0, color(black)  fintensity(0) lwidth(medthick)) ///
(bar welfare_housing g1, color("$blue")) ///
(bar welfare_debt g2, color("$red")) ///
(bar welfare_deposits g3, color("$yellow")) ///
(bar welfare_equity g4, color("$green")) ///
, ysize(4) xsize(6) graphregion(color(white)) ///
ylabel(-30 "-30K"  -20 "-20K" -10 "-10K" 0 "0K" 10 "10K" 20 "20K", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
ytitle("Welfare gain", size(medlarge))  ///
xlabel(2 "Households" 8 "Government" 14 "Foreigners", noticks) ///
legend(order(1 "Total" 2 "Housing" 3 "Debt" 4 "Deposits" 5 "Equity") rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/FigureA4.$suffix", replace

/***************************************************************************************************
Table A6: Welfare gains across sectors using heterogeneous price indices
***************************************************************************************************/

foreach method in "homogeneous" "heterogeneous"{
	use "$MyPath/save/bysector_welfare_`method'", clear
	keep seci welfare*
	gather welfare*
	gen type = string(seci)
	drop seci
	reshape wide value, i(variable) j(type) string
	local i = 0
	gen order = .
	gen label = ""
	replace label = "Housing" if variable == "welfare_housing"
	replace order = `++i' if variable == "welfare_housing"
	replace label = "\noalign{\vskip 1.5mm} Debt" if variable == "welfare_debt"
	replace order = `++i' if variable == "welfare_debt"
	replace label = "\quad Household debt" if variable == "welfare_debt_household"
	replace order = `++i' if variable == "welfare_debt_household"
	replace label = "\quad Corporate debt" if variable == "welfare_debt_corporate"
	replace order = `++i' if variable == "welfare_debt_corporate"
	replace label = "\quad Government debt" if variable == "welfare_debt_government"
	replace order = `++i' if variable == "welfare_debt_government"
	replace label = "\quad Foreign debt" if variable == "welfare_debt_foreign"
	replace order = `++i' if variable == "welfare_debt_foreign"
	replace label = "\noalign{\vskip 1.5mm} Deposits" if variable == "welfare_deposits"
	replace order = `++i' if variable == "welfare_deposits"
	replace label = "\quad Corporate deposits" if variable == "welfare_deposits_corporate"
	replace order = `++i' if variable == "welfare_deposits_corporate"
	replace label = "\quad Government deposits" if variable == "welfare_deposits_government"
	replace order = `++i' if variable == "welfare_deposits_government"
	replace label = "\quad Foreign deposits" if variable == "welfare_deposits_foreign"
	replace order = `++i' if variable == "welfare_deposits_foreign"
	replace label = "\noalign{\vskip 1.5mm} Equity" if variable == "welfare_equity"
	replace order = `++i' if variable == "welfare_equity"
	replace label = "\quad Corporate equity" if variable == "welfare_equity_corporate"
	replace order = `++i' if variable == "welfare_equity_corporate"
	replace label = "\quad Foreign equity" if variable == "welfare_equity_foreign"
	replace order = `++i' if variable == "welfare_equity_foreign"
	replace label = "\noalign{\vskip 1.5mm} Total" if variable == "welfare_total"
	replace order = `++i' if variable == "welfare_total"
	gen total = abs(value1 + value2 + value3)
	assert total < 1e-4
	sort order
	format value* total %3.1fc
	if "`method'" == "homogeneous"{
		listtab label value1 value2 value3 total if !missing(order) using  "$MyPath/tables/TableA5.tex", rstyle(tabular)  replace
	}
	else{
		replace label = label + "$^\dagger$" if regexm(label, "Foreign") | inlist(label, "Debt", "Deposits", "Equity", "Total")
		assert total < 1e-4
		format value* total %3.1fc
		sort order
		listtab label value1 value2 value3 total using  "$MyPath/tables/TableA6.tex", rstyle(tabular)  replace
	}
}
